package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.well;/**
 * Created by Administrator on 2017/3/13.
 */

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.WellMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Employee;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Organization;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Station;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Well;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.station.JsmStationService;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.util.OrgLevelUtils;
import com.cqhilink.iot.jobstandardmanagement.facade.service.well.JsmWellService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 井接口service实现类
 * @CreateDateTime 2017-03-13 10:52
 **/

public class JsmWellServiceImpl implements JsmWellService {
    @Autowired
    private WellMapper wellMapper;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private JsmStationService stationService;

    /**
     * 根据ID物理删除
     *
     * @param wellId
     * @return
     * @Author 卢曼成
     */
    @Override
    public int deleteByPrimaryKey(String wellId) {
        return wellMapper.deleteByPrimaryKey(wellId);
    }

    /**
     * 根据ID逻辑删除
     *
     * @param wellId
     * @return
     * @Author 卢曼成
     */
    @Override
    public int updateStatus(String wellId) {
        return wellMapper.updateStatus(wellId);
    }

    /**
     * 新增
     *
     * @param record
     * @return
     * @Author 卢曼成
     */
    @Override
    public int insert(Well record) {
        return wellMapper.insert(record);
    }

    /**
     * 非空新增
     *
     * @param record
     * @return
     * @Author 卢曼成
     */
    @Override
    public int insertSelective(Well record) {
        return wellMapper.insertSelective(record);
    }

    /**
     * 根据ID获取信息
     *
     * @param wellId
     * @return
     * @Author 卢曼成
     */
    @Override
    public Well selectByPrimaryKey(String wellId) {
        return wellMapper.selectByPrimaryKey(wellId);
    }

    /**
     * 非空修改
     *
     * @param record
     * @return
     * @Author 卢曼成
     */
    @Override
    public int updateByPrimaryKeySelective(Well record) {
        return wellMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return
     * @Author 卢曼成
     */
    @Override
    public int updateByPrimaryKey(Well record) {
        return wellMapper.updateByPrimaryKey(record);
    }

    /**
     * 条件查询集合
     *
     * @param record
     * @return List<Well>
     * @Author 卢曼成
     */
    @Override
    public List<Well> selectByPrimaryList(Well record) {
        return wellMapper.selectByPrimaryList(record);
    }

    /**
     * @Author 卢曼成
     * @Description 添加修改
     * @Date 2017/3/9 15:23
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> saveOrUpdate(Well record, Employee employee) throws Exception {
        String userId = employee.getEmployeeId();
        Date date = new Date();
        record.setBsflag("0");
        int num = 0;
        Station station = stationService.selectByPrimaryKey(record.getOrgId());
        Organization org = new Organization();
        if (station != null) {
            record.setOrgId(station.getOrgId());
            record.setStationId(station.getStationId());
            org.setParentOrgId(station.getStationId());
        } else {
            Organization orgs = organizationService.findOrgById(record.getOrgId());
            if (orgs != null) {
                record.setOrgId(orgs.getOrgId());
                org.setParentOrgId(orgs.getOrgId());
            } else {
                throw new Exception("井上级组织错误");
            }
        }
        org.setOrgName(record.getWellCommonName());
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(record.getWellId())) {
            org.setOrgId(record.getWellId());
            org.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
            org.setOperateId(userId);
            //井数据
            record.setUpdateDate(date);
            record.setUpdateUserId(userId);
            organizationService.updateByPrimaryKeySelective(org);
            num = updateByPrimaryKey(record);
            map.put("state", num > 0 ? RespCodeEnum.RESP_CODE_ENUM_true.code() : RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", num > 0 ? BackMessage.MODIFY_OK : BackMessage.MODIFY_FAIL);
        } else {
            String id = UniqueUtil.uuid();
            org.setOrgId(id);
            org.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
            org.setCreatorId(userId);
            org.setOrgLevelId(OrgLevelUtils.ORG_LEVEL_WELL);
            record.setCreateDate(date);
            record.setCreateUserId(userId);
            record.setWellId(id);
            organizationService.insert(org);
            num = this.insert(record);
            map.put("state", num > 0 ? RespCodeEnum.RESP_CODE_ENUM_true.code() : RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", num > 0 ? BackMessage.ADD_OK : BackMessage.ADD_FAIL);
        }
        return map;
    }

    /**
     * @param record
     * @return
     * @Author 卢曼成
     * @Description 根据条件查询树形
     */
    @Override
    public List<Well> selectByTree(Well record) {
        return wellMapper.selectByTree(record);
    }

    /**
     * @param record
     * @return
     * @Author 卢曼成
     * @Description 根据条件查询当前下级总数
     */
    @Override
    public List<Well> selectByItem(Well record) {
        return wellMapper.selectByItem(record);
    }


    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<Well> list) {
        return wellMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<Well> list) {
        return wellMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:18
     */
    @Override
    public int deleteList(List<Well> list) {
        return wellMapper.deleteList(list);
    }
}
